import { useEffect } from "react"
import TaskProcess from "./core/TaskProcess"

export interface useTaskProcessConfig {
  onError?: any
  onRun?: any
  onProcess?: any
  onPause?: any
  onAbort?: any
  onSuccess?: any
}
export interface useTaskProcessConfig {}
export default function useTaskProcess(
  task: TaskProcess,
  config: useTaskProcessConfig,
) {
  const { onError, onAbort, onPause, onProcess, onSuccess, onRun } = config
  useEffect(() => {
    onError && (task.onerror = onError)
    onAbort && (task.onabort = onAbort)
    onPause && (task.onpause = onPause)
    onProcess && (task.onprocess = onProcess)
    onRun && (task.onrun = onRun)
    onSuccess && (task.onsuccess = onSuccess)
    return () => {
      onError && task.remove("error", onError)
      onAbort && task.remove("abort", onAbort)
      onPause && task.remove("pause", onPause)
      onProcess && task.remove("process", onProcess)
      onRun && task.remove("run", onRun)
      onSuccess && task.remove("success", onSuccess)
    }
  }, [])

  return { task }
}
